﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0
 *  ----------------------------------------------------------------------------
 *  File:       GameScreen.cs
 *  Author:     Shane-PC\Shane
 *  
 *  Credits:    Sean James's Game Engine Tutorial (See Below)
 *  
 *  Tutorial:   'http://www.innovativegames.net/blog/2009/10/20/game-engine-tutorial-part-iii-chapter-2-gamescreen/'
 *  
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;

namespace EuphoricEngine
{
    public class GameScreen
    {
        private bool _loaded = false;
        private Engine _engine = null;
        private List<Component> _components = new List<Component>();

        public virtual void Update()
        {
            List<Component> updating = new List<Component>();

            foreach (Component c in _components)
                updating.Add(c);

            foreach (Component c in updating)
                c.Update();
        }

        public virtual void Draw()
        {
            foreach (Component c in _components)
                c.Draw();
        }

        public void LoadGameScreen()
        {
            if (_loaded)
                return;

            _loaded = true;

            Load();
        }

        protected virtual void Load()
        {
        }

        public void AddComponent(Component component)
        {
            if (!_components.Contains(component))
            {
                _components.Add(component);
                component.Parent = this;
                component.LoadComponent();
                PutComponentInOrder(component);
            }
        }

        public void RemoveComponent(string Name)
        {
            Component component = this[Name];
            RemoveComponent(component);
        }

        public void RemoveComponent(Component component)
        {
            if (component != null && _components.Contains(component))
            {
                _components.Remove(component);
                component.Parent = null;
            }
        }

        public void PutComponentInOrder(Component component)
        {
            if (_components.Contains(component))
            {
                _components.Remove(component);

                int i = 0;
                for (i = 0; i < _components.Count; i++)
                    if (_components[i].DrawOrder >= component.DrawOrder)
                        break;

                _components.Insert(i, component);
            }
        }

        public Component this[string Name]
        {
            get
            {
                foreach (Component component in _components)
                {
                    if (component.Name == Name)
                        return component;
                }

                return null;
            }
        }

        public Engine Engine
        {
            get { return _engine; }
            set { _engine = value; }
        }

        public bool Loaded
        {
            get { return _loaded; }
        }
    }
}
